<?php
namespace Admin\Controller;
use Think\Controller;
class HostController extends AuthController {
    
   public function _initialize(){
         
		 parent::_initialize();
         
        $hosttype = array('独立服务器','虚拟主机','云虚拟主机');
		$this->assign(array(
			'hosttype' => $hosttype,
			'now'      => time(),
		));
    }

    public function index(){
	    echo "hello world!";
        $this->display();
    }
	
    public function vps(){
	    $vps      = M("vps");
		$account  = M("account");
		import("ORG.Util.Page");// 导入分页类
		$count      = $vps->count();
		$page     = new \Think\Page($count,10);//tp3.2新增
		$show     = $page->show();
		$limit    = $page->firstRow.','.$page->listRows;
		
		$list     = $vps   	->join("abc_account as b on abc_vps.aid = b.aid")
		                    -> order("abc_vps.listorder desc, abc_vps.vid desc") 
				            -> limit($limit)->select();
		$account  = $account-> order('listorder desc, aid desc')
		                    -> select();
		
		$this->assign(array(
		    'list'   => $list,
			'page'   => $show,
			'account'=> $account,
		));
        $this->display();
    }
	
	/*网站内容编辑*/
	public function vps_edit(){
	    $vid        = I('get.vid');
		$vps        = M("vps");
		$account    = M("account");
		$where      = array('vid' => $vid);
		if (!empty($vid)){
		     $arr = $vps   -> where('vid = '.$vid.'') 
						   -> order('vid desc') 
						   -> find();
		}else{
		   $this->error("参数传递错误");
		}
		$remote    = json_decode($arr['remote'], true);//远程账号
		$superftp  = json_decode($arr['superftp'], true);//ftp账号

		$account   = $account -> order('listorder desc,aid desc') 
							  -> select();
		$this->assign(array(
				'data'        => $arr,
				'account'     => $account,
				'remote'      => $remote,
				'superftp'    => $superftp,
		));
        $this->display();
    }
	
	/*VPS信息更新与添加*/
	public function vps_update(){
	    $vid = I('get.vid');
	    $vps = M("vps");
		
        if (IS_POST){
			$data['vpsname']   =   I('post.vpsname');
			$data['aid']       =   I('post.account');
			$data['vpsip']     =   I('post.vpsip','','htmlspecialchars');
			$data['jifang']    =   I('post.jifang','','htmlspecialchars');
			$data['status']    =   I('post.status') ? I('post.status') : 1;
			$data['cpu']       =   I('post.cpu','','htmlspecialchars');
			$data['memory']    =   I('post.memory','','htmlspecialchars');
			$data['harddisk']  =   I('post.harddisk','','htmlspecialchars');
			$data['bandwidth'] =   I('post.bandwidth','','htmlspecialchars');
			$data['remote']    =   json_encode(I('post.remote','','htmlspecialchars'));
			$data['superftp']  =   json_encode(I('post.superftp','','htmlspecialchars'));
			$data['vpsstart']  =   strtotime(I('post.vpsstart','','htmlspecialchars'));
			$data['vpsend']    =   strtotime(I('post.vpsend','','htmlspecialchars'));
			$data['content']   =   I('post.content','','htmlspecialchars');
			$data['inputtime'] =   I('post.inputtime') ? strtotime(I('post.inputtime')) : time();
			$data['updatetime'] =   time();
			$data['listorder'] =   I('post.listorder','','htmlspecialchars');
	    }
	   
	   if (!isset($vid) || $vid ==""){//id为空表示新增
		    $count = $vps -> add($data);
			if($count){
				 $this->success("添加成功",U('Host/vps'));
			}else{
				  $this->error("添加失败");	
			}
	   }else{//修改信息
		    $where['vid'] = $vid;
		    $count = $vps ->where($where) -> save($data);
			if($count!== false){
				 $this->success("修改成功",U('Host/vps'));
			}else{
				    $this->error("修改失败");	
			}
	   
	   }
        
    }
	
	/*VPS快速续费更新*/
	public function vpsfast_update(){
	    $vid          = I('get.vid');
	    $vps          = M("vps");
		$where['vid'] = $vid;
		$arr = $vps -> where($where) -> find();
		if($arr){
		    $data['vpsend'] = strtotime("+1 year",$arr['vpsend']);
			$arr2 = $vps -> where($where) -> save($data);
			if($arr2){
			    $this->redirect('Host/vps','', 1,'续费更新中...');
			}else{
			     $this->error("更新失败");
			}
		}else{
		    $this->error("参数错误",'vps');
		}
	}
	
	/*一下是虚拟主机管理*/
	public function host(){
	    $host     = M("host");
		$account  = M("account");
		import("ORG.Util.Page");// 导入分页类
		$count    = $host->count();
		$page     = new \Think\Page($count,10);//tp3.2新增
		$show     = $page->show();
		$limit    = $page->firstRow.','.$page->listRows;
		
		$list     = $host  	->join("abc_account as b on abc_host.aid = b.aid")
		                    -> order("abc_host.listorder desc, abc_host.hid desc") 
				            -> limit($limit)->select();
		$account  = $account-> order('listorder desc, aid desc')
		                    -> select();
		
		foreach($list as $k => $arr){
		    $ftp   = json_decode($arr['ftp']);
			$panel = json_decode($arr['panel']);
			$list[$k]['ftp'] = $ftp;
			$list[$k]['panel'] = $panel;
			//print_r($list[$k]['ftp']);
		}
		//print_r($list);
		$this->assign(array(
		    'list'   => $list,
			'page'   => $show,
			'account'=> $account,
		));
        $this->display();
    }
	
	/*虚拟主机内容编辑*/
	public function host_edit(){
	    $hid        = I('get.hid');
		$host       = M("host");
		$account    = M("account");
		$where      = array('hid' => $hid);
		if (!empty($hid)){
		     $arr = $host  -> where('hid = '.$hid.'') 
						   -> order('listorder desc, hid desc') 
						   -> find();
		}else{
		   $this->error("参数传递错误");
		}
		$ftp       = json_decode($arr['ftp'], true);//FTP
		$database  = json_decode($arr['database'], true);//数据库账号
		$panel     = json_decode($arr['panel'], true);//控制面板账号
		$account   = $account -> order('listorder desc,aid desc') 
							  -> select();
		$this->assign(array(
				'data'        => $arr,
				'account'     => $account,
				'ftp'         => $ftp,
				'database'    => $database,
				'panel'       => $panel,
		));
        $this->display();
    }
	
	/*虚拟主机信息更新与添加*/
	public function host_update(){
	    $hid  = I('get.hid');
	    $host = M("host");
		
        if (IS_POST){
			$data['hostname']  =   I('post.hostname','','htmlspecialchars');
			$data['aid']       =   I('post.aid');
			$data['hosturl']   =   I('post.hosturl','','htmlspecialchars');
			$data['hostip']    =   I('post.hostip','','htmlspecialchars');
			$data['cname']     =   I('post.cname','','htmlspecialchars');
			$data['jifang']    =   I('post.jifang','','htmlspecialchars');
			$data['hostos']    =   I('post.hostos','','htmlspecialchars');
			$data['status']    =   I('post.status') ? I('post.status') : 1;
			$data['hoststart'] =   strtotime(I('post.hoststart','','htmlspecialchars'));
			$data['hostend']   =   strtotime(I('post.hostend','','htmlspecialchars'));
			
			$data['space']     =   I('post.space','','htmlspecialchars');
			$data['ftp']       =   json_encode(I('post.ftp','','htmlspecialchars'));
			$data['database']  =   json_encode(I('post.database','','htmlspecialchars'));
			$data['panel']     =   json_encode(I('post.panel','','htmlspecialchars'));
			$data['inputtime'] =   I('post.inputtime') ? strtotime(I('post.inputtime')) : time();
			$data['listorder'] =   I('post.listorder','','htmlspecialchars');
	    }
	   
	   if (!isset($hid) || $hid ==""){//id为空表示新增
		    $count = $host -> add($data);
			if($count){
				 $this->success("添加成功",U('Host/host'));
			}else{
				  $this->error("添加失败");	
			}
	   }else{//修改信息
		    $where['hid'] = $hid;
		    $count = $host ->where($where) -> save($data);
			if($count!== false){
				 $this->success("修改成功",U('Host/host'));
			}else{
				    $this->error("修改失败");	
			}
	   
	   }
        
    }
	
	/*VPS快速续费更新*/
	public function hostfast_update(){
	    $hid          = I('get.hid');
	    $host          = M("host");
		$where['hid'] = $hid;
		$arr = $host -> where($where) -> find();
		if($arr){
		    $data['hostend'] = strtotime("+1 year",$arr['hostend']);
			$arr2 = $host -> where($where) -> save($data);
			if($arr2){
			    $this->redirect('Host/host','', 1,'续费更新中...');
			}else{
			     $this->error("更新失败");
			}
		}else{
		    $this->error("参数错误",'host');
		}
	}
	
	/*ajax检测查看密码权限*/
	public function check_pwd(){
	    $pwd = I("post.password");
		$user2 = M("user2");
		$userid = $_SESSION['Aid'];
		$data = 0;
		$where['id'] = $userid;
		$arr = $user2 ->where($where) ->find();
		if($arr){
		     if(md5($pwd) == $arr['password'] && $arr['admintype'] == 0){
			    $data = 1;
			 }else{
			     $data = 0;
			 }
		}else{
		    $data = 0;
		}
		$this->ajaxReturn($data); 
	}
	
	/*导出VPS*/
	public function export_vps(){
	    $vps       = M("vps");
		$accounts  = M("account");
		
		$list      = $vps   	->join("left join abc_account as b on abc_vps.aid = b.aid")
		                        -> order("abc_vps.listorder desc, abc_vps.vid desc") 
				                -> limit($limit)->select();
		$account   = $accounts  -> order('listorder desc, aid desc')
		                        -> select();
		
		Vendor('PHPExcel.PHPExcel');
		$objExcel = new \PHPExcel();
        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
		
		$objExcel->getActiveSheet()->setCellValue('A1','ID');
		$objExcel->getActiveSheet()->setCellValue('B1', "VPS名称");
		$objExcel->getActiveSheet()->setCellValue('C1', "IP地址");
		$objExcel->getActiveSheet()->setCellValue('D1', "机房");
		$objExcel->getActiveSheet()->setCellValue('E1', "状态");
		$objExcel->getActiveSheet()->setCellValue('F1', "配置（CPU丨内存丨硬盘丨带宽）");
		$objExcel->getActiveSheet()->setCellValue('G1', "到期时间");
		$objExcel->getActiveSheet()->setCellValue('H1', "所在账户");
		$objExcel->getActiveSheet()->setCellValue('I1', "远程账号");
		$objExcel->getActiveSheet()->setCellValue('J1', "超级FTP");
		
		//设置当前的sheet
		$objExcel->setActiveSheetIndex(0);
		//设置sheet的name
		$objExcel->getActiveSheet()->setTitle('全部VPS列表');
		
		//文字对齐方式  锚：bbb
	    $objExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objExcel->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);  
	
		//设置默认行高
        $objExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(30);
		
		//设置填充颜色
        $objExcel->getActiveSheet()->getStyle('A1:L1')->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
        $objExcel->getActiveSheet()->getStyle('A1:L1')->getFill()->getStartColor()->setARGB('#009688');
	   
	   /*设置字体颜色*/
	   $objExcel->getActiveSheet()->getStyle('A1:l1')->getFont()->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_WHITE);
	   $objExcel->getActiveSheet()->getStyle('E')->getFont()->getColor()->setARGB('#009688');
	   
	   // 高置列的宽度
		$objExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
		$objExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
		$objExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
		$objExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
		$objExcel->getActiveSheet()->getColumnDimension('E')->setWidth(8);
		$objExcel->getActiveSheet()->getColumnDimension('F')->setWidth(40);
		$objExcel->getActiveSheet()->getColumnDimension('G')->setWidth(25);
		$objExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20);
		$objExcel->getActiveSheet()->getColumnDimension('I')->setWidth(45);
		$objExcel->getActiveSheet()->getColumnDimension('J')->setWidth(35);

	    
		$count = count($list);//$driver 为数据库表取出的数据
		for ($i = 2; $i <= $count+1; $i++) {
			$stat    = $list[$i-2]['status'] ? "正常" : "过期";
			$peizhi  = "CPU:".$list[$i-2]['cpu']."核  内存:".$list[$i-2]['memory']."G  硬盘:"
			           .$list[$i-2]['harddisk']."G  带宽:".$list[$i-2]['bandwidth']."M";
			
		    $objExcel->getActiveSheet()->setCellValue('A' . $i, $list[$i-2]['vid']); 
		    $objExcel->getActiveSheet()->setCellValue('B' . $i, $list[$i-2]['vpsname']); 
		    $objExcel->getActiveSheet()->setCellValue('C' . $i, $list[$i-2]['vpsip']); 
		    $objExcel->getActiveSheet()->setCellValue('D' . $i, $list[$i-2]['jifang']);
			$objExcel->getActiveSheet()->setCellValue('E' . $i, $stat);
			$objExcel->getActiveSheet()->setCellValue('F' . $i, $peizhi);
			$objExcel->getActiveSheet()->setCellValue('G' . $i, date('Y-m-d',$list[$i-2]['vpsstart'])."  ~ ".date('Y-m-d',$list[$i-2]['vpsend']));
			$objExcel->getActiveSheet()->setCellValue('H' . $i, $list[$i-2]['aname']);
			$objExcel->getActiveSheet()->setCellValue('I' . $i, $list[$i-2]['remote']);
			$objExcel->getActiveSheet()->setCellValue('J' . $i, $list[$i-2]['superftp']);
		}
	   
	   $objExcel->setActiveSheetIndex(); 
	   $excel_name = "VPS列表".date('Y-m-d',time());
	   header('Content-Type: applicationnd.ms-excel'); 
	   header('Content-Disposition: attachment;filename='.$excel_name.'.xls'); 
	   header('Cache-Control: max-age=0'); 
		
	   $objWriter->save('php://output'); 
	   exit;
	}
	
	/*导出虚拟主机*/
	public function export_host(){
	    $host       = M("host");
		$accounts  = M("account");
		
		$list      = $host      ->join("abc_account as b on abc_host.aid = b.aid")
		                        -> order("abc_host.listorder desc, abc_host.hid desc") 
				                -> limit($limit)->select();
		$account   = $accounts  -> order('listorder desc, aid desc')
		                        -> select();
		
		Vendor('PHPExcel.PHPExcel');
		$objExcel = new \PHPExcel();
        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
		
		$objExcel->getActiveSheet()->setCellValue('A1','ID');
		$objExcel->getActiveSheet()->setCellValue('B1', "主机名称");
		$objExcel->getActiveSheet()->setCellValue('C1', "绑定域名");
		$objExcel->getActiveSheet()->setCellValue('D1', "机房");
		$objExcel->getActiveSheet()->setCellValue('E1', "状态");
		$objExcel->getActiveSheet()->setCellValue('F1', "IP");
		$objExcel->getActiveSheet()->setCellValue('G1', "到期时间");
		$objExcel->getActiveSheet()->setCellValue('H1', "所在账户");
		$objExcel->getActiveSheet()->setCellValue('I1', "解析别名");
		$objExcel->getActiveSheet()->setCellValue('J1', "FTP");
		$objExcel->getActiveSheet()->setCellValue('K1', "操作系统");
		$objExcel->getActiveSheet()->setCellValue('L1', "空间大小");
		$objExcel->getActiveSheet()->setCellValue('M1', "控制面板");
		$objExcel->getActiveSheet()->setCellValue('N1', "数据库");
		
		//设置当前的sheet
		$objExcel->setActiveSheetIndex(0);
		//设置sheet的name
		$objExcel->getActiveSheet()->setTitle('虚拟主机列表');
		
		//文字对齐方式  锚：bbb
	    $objExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objExcel->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);  
	
		//设置默认行高
        $objExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(30);
		
		//设置填充颜色
        $objExcel->getActiveSheet()->getStyle('A1:N1')->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
        $objExcel->getActiveSheet()->getStyle('A1:N1')->getFill()->getStartColor()->setARGB('#009688');
	   
	   /*设置字体颜色*/
	   $objExcel->getActiveSheet()->getStyle('A1:N1')->getFont()->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_WHITE);
	   $objExcel->getActiveSheet()->getStyle('E')->getFont()->getColor()->setARGB('#009688');
	   
	   // 高置列的宽度
		$objExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
		$objExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
		$objExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
		$objExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
		$objExcel->getActiveSheet()->getColumnDimension('E')->setWidth(10);
		$objExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
		$objExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
		$objExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
		$objExcel->getActiveSheet()->getColumnDimension('I')->setWidth(40);
		$objExcel->getActiveSheet()->getColumnDimension('J')->setWidth(80);
		$objExcel->getActiveSheet()->getColumnDimension('K')->setWidth(10);
		$objExcel->getActiveSheet()->getColumnDimension('L')->setWidth(8);
		$objExcel->getActiveSheet()->getColumnDimension('M')->setWidth(80);
		$objExcel->getActiveSheet()->getColumnDimension('N')->setWidth(80);

	    
		$count = count($list);//$driver 为数据库表取出的数据
		for ($i = 2; $i <= $count+1; $i++) {
			$stat    = $list[$i-2]['status'] ? "正常" : "过期";		
		    $objExcel->getActiveSheet()->setCellValue('A' . $i, $list[$i-2]['hid']); 
		    $objExcel->getActiveSheet()->setCellValue('B' . $i, $list[$i-2]['hostname']); 
		    $objExcel->getActiveSheet()->setCellValue('C' . $i, $list[$i-2]['hosturl']); 
		    $objExcel->getActiveSheet()->setCellValue('D' . $i, $list[$i-2]['jifang']);
			$objExcel->getActiveSheet()->setCellValue('E' . $i, $stat);
			$objExcel->getActiveSheet()->setCellValue('F' . $i, $list[$i-2]['hostip']);
			$objExcel->getActiveSheet()->setCellValue('G' . $i, date('Y-m-d',$list[$i-2]['hostend']));
			$objExcel->getActiveSheet()->setCellValue('H' . $i, $list[$i-2]['aname']);
			$objExcel->getActiveSheet()->setCellValue('I' . $i, $list[$i-2]['cname']);
			$objExcel->getActiveSheet()->setCellValue('J' . $i, $list[$i-2]['ftp']);
			$objExcel->getActiveSheet()->setCellValue('K' . $i, $list[$i-2]['hostos']);
			$objExcel->getActiveSheet()->setCellValue('L' . $i, $list[$i-2]['space']);
			$objExcel->getActiveSheet()->setCellValue('M' . $i, $list[$i-2]['panel']);
			$objExcel->getActiveSheet()->setCellValue('N' . $i, $list[$i-2]['database']);

		}
	   
	   $objExcel->setActiveSheetIndex(); 
	   $excel_name = "虚拟主机".date('Y-m-d',time());
	   header('Content-Type: applicationnd.ms-excel'); 
	   header('Content-Disposition: attachment;filename='.$excel_name.'.xls'); 
	   header('Cache-Control: max-age=0'); 
		
	   $objWriter->save('php://output'); 
	   exit;
	}
	
}